﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Design;
using System.Linq;
using System.Web;

namespace Galaktika.BI.Silverlight.ClientApp.Web
{
    using Galaktika.BI.Silverlight.ClientApp.Web.DAL;
    using Galaktika.BI.Silverlight.ClientApp.Web.Runtime.Services;

    public static class AppManager
    {
        private static DalRootContainer m_Inst;
        public static DalRootContainer Instance
        {
            get
            {
                lock (syncRoot)
                {
                    if (m_Inst == null)
                    {
                        Create();
                    }
                }
                return m_Inst;
            }
        }

        private static IServiceProvider m_Services;
        public static IServiceProvider Services
        {
            get
            {
                return m_Services;
            }
        }

        private static object syncRoot = new object();
        private static void Create()
        {
            IAppLoader loader = AppLoaderFactory.CreateLoader();
            m_Inst = loader.Load();

            ServiceContainer sc = new ServiceContainer();
            sc.AddService(typeof(IExecutionContext), new ExecutionContext());
            sc.AddService(typeof(IServiceContainer), sc);
            m_Services = sc;
        }

        public static void Dispose()
        {
            m_Inst = null;
        }
    }
}
